﻿using System.Collections.Generic;

namespace GraphQlSql
{
    /// <summary>
    /// 动态sql查询类   可以为  select 1 from user left join role on user.id=role.userid where user.id>0 and role.id=user.roleid an roleid in (1,2,3,4)
    /// 不支持子查询 slect 1 from user where id ( select roleid from role)  也不支持on多条件  如 on a.id=b.id and a.name=b.name
    /// </summary>
    public class GraphQlSqlModel
    {
        /// <summary>
        /// 主表
        /// </summary>
        public string MainTableName { get; set; }

        /// <summary>
        /// 主查询条件
        /// </summary>

        public List<WhereCondition> Wheres { get; set; }

        /// <summary>
        /// 表关联
        /// </summary>

        public List<TableRef> TableRefs { get; set; }

        public List<FieldLt> SelectFields { get; set; }

        public List<OrderCondition> OrderCondition { get; set; }
        /// <summary>
        /// 分页索引 1开始 默认1
        /// </summary>

        public int PageIndex { get; set; } = 1;

        private int _pageSize = 10;

        /// <summary>
        /// 分页大小  默认10
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = _pageSize > 500 ? 50 : _pageSize; }
        }
    }

    /// <summary>
    /// 表关联类
    /// </summary>
    public class TableRef
    {
        /// <summary>
        /// 连接方式  lefe join  right join
        /// </summary>
        public string LinkType { get; set; }

        /// <summary>
        /// 连接的表名
        /// </summary>

        public string LinkTableName { get; set; }

        /// <summary>
        /// 表关联on条件集合
        /// </summary>
        public List<OnCondition> OnConditions { get; set; }
    }

    /// <summary>
    /// 表关联on条件集合
    /// </summary>
    public class OnCondition
    {
        /// <summary>
        /// 关联的左表名
        /// </summary>
        public string LeftTableName { get; set; }

        /// <summary>
        /// 关联的左表字段
        /// </summary>
        public string LeftTableField { get; set; }

        /// <summary>
        /// 关联的右表名
        /// </summary>
        public string RightTableName { get; set; }

        /// <summary>
        /// 关联的右表字段
        /// </summary>
        public string RightTableField { get; set; }
    }

    /// <summary>
    /// where条件类
    /// </summary>
    public class WhereCondition
    {
        /// <summary>
        /// 逻辑符号 如 and  or
        /// </summary>
        public string LogicSymbol { get; set; }

        /// <summary>
        /// 关联的左表名  如果是字段间的比较，如 user.id=role.userid   则此值为user
        /// </summary>
        public string LeftTableName { get; set; }

        /// <summary>
        /// 关联的左表字段  如果是字段间的比较，如 user.id=role.userid   则此值为id
        /// </summary>
        public string LeftTableField { get; set; }

        /// <summary>
        /// 条件符号 如> = <
        /// </summary>
        public string ConditionSymbol { get; set; }

        /// <summary>
        /// 关联的右表名    如果是字段间的比较，如 user.id=role.userid   则此值为role
        /// </summary>
        public string RightTableName { get; set; }

        /// <summary>
        /// 关联的右表字段 如果是字段间的比较，如 user.id=role.userid   则此值为userid
        /// </summary>
        public string RightTableField { get; set; }

        /// <summary>
        /// 条件比较的值。如果不是字段的比较 而是值几个比较 用这个。 比如  id in (1,23,4)  则 此值为1,23,4
        /// </summary>
        public string ConditionValue { get; set; }

        /// <summary>
        /// 扩展查询   即 带大括号的部分 比如  where  a=3  and  (  b=5 or c=6 ) .此集合返回  and  (  b=5 or c=6 )  有此条件 则忽略其他字段（只有ExtendWhere 和 LogicSymbol 起作用 ）
        /// </summary>
        public List<WhereCondition> ExtendWhere { get; set; }
    }

    /// <summary>
    /// 要返回的字段  暂时不支持聚合函数
    /// </summary>
    public class FieldLt
    {
        public FieldLt()
        {
        }

        /// <summary>
        ///表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 字段名集合
        /// </summary>

        public List<FieldInfo> Fields { get; set; }
    }
    /// <summary>
    /// 排序条件
    /// </summary>
    public class OrderCondition
    {
        public OrderCondition()
        {
        }

        /// <summary>
        ///表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 字段名集合
        /// </summary>

        public string Fields { get; set; }
        /// <summary>
        /// asc  desc
        /// </summary>
        public string   OrderByType{ get; set; }


    }
    public class SqlResult
    {
        public bool IsOk { get; set; } = true;

        public string Message { get; set; } = "";

        public string SqlString { get; set; } = "";
    }

    /// <summary>
    /// 表和字段的信息  sqlserver和mysql  ：SELECT * FROM INFORMATION_SCHEMA.columns   postgresql为 select *  from  pg_class
    /// </summary>
    public class TbAndColumnInfo
    {
        public TbAndColumnInfo()
        {
        }

        public TbAndColumnInfo(string tableName, string columnName, string columnType)
        {
            this.TableName = tableName;
            this.ColumnName = columnName;
            this.ColumnType = columnType;
        }

        /// <summary>
        /// /表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 字段名称
        /// </summary>
        public string ColumnName { get; set; }

        /// <summary>
        /// 字段类型
        /// </summary>
        public string ColumnType { get; set; }
    }

    /// <summary>
    /// 返回的字段信息
    /// </summary>
    public class FieldInfo
{
        /// <summary>
        /// 字段名
        /// </summary>
        public string FieldName { get; set; }
        /// <summary>
        /// 字段别名
        /// </summary>
        public string AliasName { get; set; }

    }

}